package com.sk.cardeal.act.home.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.CostType;
import com.sk.cardeal.entity.OcrInfo;
import com.sk.cardeal.entity.Project;
import com.sk.cardeal.entity.ProjectListBean;
import com.sk.cardeal.entity.WorkPostion;
import com.sk.cardeal.entity.WorkPostionList;
import com.sk.cardeal.entity.Worker;
import com.sk.cardeal.entity.WorkerListBean;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.FileUtil;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.utils.RecognizeService;
import com.sk.cardeal.utils.SnackUtils;
import com.sk.cardeal.view.LineMenuView;
import com.sk.cardeal.view.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sk.cardeal.common.Constants.CAR_CARD;
import static com.sk.cardeal.common.Constants.CHOOSE_FIX_PEP;
import static com.sk.cardeal.common.Constants.CHOOSE_PROJECT;
import static com.sk.cardeal.common.Constants.CHOOSE_WORK_POSTION;
import static com.sk.cardeal.common.Constants.OTHER_PAY;

/**
 * Created by sk on 2018/4/9.
 */

public class ChangeOrderToPepActivity extends BaseActivity implements HandleDataCallBack {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.form_num)
    TextView formNum;
    @BindView(R.id.car_num)
    TextView carNum;
    @BindView(R.id.fix_type)
    TextView fixType;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.work_seat)
    LineMenuView workSeat;
    @BindView(R.id.technician_choose)
    LineMenuView technicianChoose;
    @BindView(R.id.send_form)
    TextView sendForm;
    private String id;
    private LoadingDialog loadingDialog;
    private String placeId, technicianId;

    @Override
    protected int getContentView() {
        return R.layout.activity_change_order_to_pep;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleView.setMyCallBack(this);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        id = getIntent().getStringExtra("id");
    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i) {
            case 1:
                finish();
                break;
        }
    }

    @OnClick({R.id.send_form, R.id.work_seat, R.id.technician_choose})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.send_form:
                if (placeId != null && placeId.length() > 0) {
                    SnackUtils.showError(mRootView, "请选择工位！");
                    return;
                }
                if (technicianId != null && technicianId.length() > 0) {
                    SnackUtils.showError(mRootView, "请选择技师！");
                    return;
                }
                loadingDialog.show();
                AppApi.changeOrderTo(id, placeId, technicianId, ChangeOrderToPepActivity.this, 0, ChangeOrderToPepActivity.this);
                break;
            case R.id.work_seat:
                loadingDialog.show();
                AppApi.getWorkPostion(ChangeOrderToPepActivity.this, 1, ChangeOrderToPepActivity.this);
                break;
            case R.id.technician_choose:
                loadingDialog.show();
                AppApi.getTechnicianList(ChangeOrderToPepActivity.this, 2, ChangeOrderToPepActivity.this);
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                Toast.makeText(mContext,"该工单已成功转派！",Toast.LENGTH_SHORT).show();
                finish();
                break;
            case 2:
                WorkerListBean workerListBean = JSON.parseObject(result, WorkerListBean.class);
                ArrayList<Worker> workerArrayList = (ArrayList<Worker>) workerListBean.getResultList();
                Intent workIntent = new Intent(mContext, PepChooseActivity.class);
                workIntent.putParcelableArrayListExtra("data", workerArrayList);
                startActivityForResult(workIntent, CHOOSE_FIX_PEP);
                break;
            case 1:
                WorkPostionList workPostionList = JSON.parseObject(result, WorkPostionList.class);
                ArrayList<WorkPostion> workFormList = (ArrayList<WorkPostion>) workPostionList.getResultList();
                Intent intent = new Intent(mContext, ChooseWorkPostionActivity.class);
                intent.putParcelableArrayListExtra("data", workFormList);
                startActivityForResult(intent, CHOOSE_WORK_POSTION);
                break;
        }
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case CHOOSE_FIX_PEP:
                    Worker worker = data.getParcelableExtra("worker");
                    technicianChoose.setHint(worker.getPersonName());
                    technicianChoose.setHintColor(Color.parseColor("#232323"));
                    technicianId = worker.getId();
                    break;
                case CHOOSE_WORK_POSTION:
                    WorkPostion workPostion = data.getParcelableExtra("data");
                    workSeat.setHint(workPostion.getText());
                    workSeat.setHintColor(Color.parseColor("#232323"));
                    placeId = workPostion.getId();
                    break;
            }
        }
    }


}
